home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17772 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  65 lines

  1. Path: in2.uu.net!demos!usenet
  2. From: Alexey Ruzin <00alex@dbs.demos.su>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to Get all Permutations
  5. Date: Wed, 17 Apr 1996 17:52:23 +0400
  6. Organization: Demos Online Service
  7. Message-ID: <3174F797.2D8F@dbs.demos.su>
  8. References: <3174417f.15405235@sun.cis.smu.edu>
  9. NNTP-Posting-Host: 00alex@dbs.demos.su
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.4 sun4m)
  14.  
  15. Damon Bowman wrote:
  16. > I'm writing a program in which I need to fill an array with all
  17. > possible combinations of a group of numbers.
  18. > For example, say you have the following table:
  19. >  Choices - A  B  C
  20. > -------------------
  21. > Person 1 - 1  2  3
  22. > Person 2 - 2  3  1
  23. > Person 3 - 3  1  2
  24. > This is one possible combination out of 216 (#choices!)^#persons =
  25. > 3!^3 = 216.
  26. > In other words, each person must rank three choices in order of
  27. > preference.  I am trying to fill an array with every possible
  28. > combination of all three persons' choices.
  29.  
  30. If i got all right, you may do so:
  31. Write a class named 'Choice' which will represent one of all
  32. possible variants.
  33.  
  34. #define NUMCHOICES 3
  35. #define NUMPERSONS 3
  36.  
  37. class Choice
  38. {
  39. public:
  40.   int choice[NUMCHOICES];
  41. ...
  42. };
  43.  
  44. Now fill a massive of all possible choices:
  45.  
  46. Choice all[NUMCHOICES!]; /* ! factorial is not C++ method too */
  47.  
  48. Then each person have its own choice, so NUMPERSONS persons are
  49. one variant of set, and there are NUMCHOICES!^3 variants in the set:
  50.  
  51. int election[NUMPERSONS][NUMCHOICES!^3]; /* power is not C++ method */
  52.  
  53. This is better (snd simple) to use recursions to fill out these arrays.
  54.  
  55. I hope you know how to write recurse functions. If not
  56. write to conference reply on my message, I explain.
  57.  
  58. Good luck.
  59. Alex.
  60.